class Solution
{
public:
    int maxTotalReward(vector<int> &rewardValues)
    {
        sort(rewardValues.begin(), rewardValues.end());
        set<int> rewards{0};
        for (int reward : rewardValues)
        {
            auto it = rewards.lower_bound(reward);
            --it;
            while (true)
            {
                int r = *it;
                rewards.insert(r + reward);
                if (it == rewards.begin())
                {
                    break;
                }
                --it;
            }
        }
        return *rewards.rbegin();
    }
};